package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.service.WmNewsService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdChannel;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.admin.mapper.WmChannelMapper;
import com.heima.admin.service.WmChannelService;

import com.heima.model.wemedia.pojos.WmNews;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 频道名称模糊分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getChannelList(ChannelDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //构建分页条件
        Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());

        //构建查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getName() != null && StringUtils.isNotBlank(dto.getName()),
                WmChannel::getName, dto.getName());
        queryWrapper.orderByDesc(WmChannel::getOrd);
        //查询
        page = page(page, queryWrapper);

        //结果校验
        if (page == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //构建返回
        PageResponseResult pageResponseResult = new PageResponseResult(page);
        pageResponseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        pageResponseResult.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return pageResponseResult;
    }

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {

        //参数校验
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //通过id查找
        WmChannel channel = getById(adChannel.getId());

        //判断是否存在
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //判断是否启用
        if (channel.getStatus()) {
            if (!adChannel.getStatus()) {
                if (judge(adChannel.getId())) {    //ture表示有引用
                    return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "频道被引用,无法禁用");    //字段一起完成或失败
                    // adChannel.setStatus(channel.getStatus());    //启用状态不变, 其他字段改变
                }


            }
        }

        //数据复制
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);

        //更新
        try {
            updateById(wmChannel);
        } catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.FILE_STATICURL_ERROR, "更新失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        //参数校验
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //数据复制
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel, wmChannel);

        //通过频道名查找
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(wmChannel.getName()), WmChannel::getName, wmChannel.getName());
        WmChannel channel = getOne(queryWrapper);

        //判断名字是否存在
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //保存
        try {
            wmChannel.setCreatedTime(new Date());
            wmChannel.setIsDefault(true);
            save(wmChannel);
        } catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.FILE_STATICURL_ERROR, "保存失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer id) {

        //校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //通过id查找
        WmChannel channel = getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //判读是否禁用, 禁用才可以删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "频道未禁用");
        }

        //删除
        try {
            removeById(id);
        } catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.FILE_STATICURL_ERROR, "删除失败");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 判断频道是否被引用
     *
     * @param id
     * @return
     */
    private boolean judge(Integer id) {

        LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getChannelId, id);
        int count = wmNewsService.count(queryWrapper);

        if (count > 0) {
            return true;
        }
        return false;
    }
}


